package com.cnguifang.feigewallet.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.cnguifang.feigewallet.R;
import com.cnguifang.feigewallet.data.UserInfo;
import com.cnguifang.feigewallet.utils.UserInfoUtils;

import java.util.Hashtable;

public class QRCodeActivity extends BaseActivity {

    private ImageView im1;

    private TextView qrcodeText;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_qr);
        qrcodeText = findViewById(R.id.qrcodeText);
        int code_number=getIntent().getIntExtra("code_number",0);
        switch (code_number){
            case 0:
                qrcodeText.setText("扫一扫上面的二维码图案，加我缅信");
                createQRcodeImage("https://a.feigecb.com/?:add".concat(UserInfoUtils.getUserName(this)));
                break;
            case 1:
                qrcodeText.setText("扫一扫上面的二维码图案，缅信收款码");
                createQRcodeImage("https://a.feigecb.com/?:for".concat(UserInfoUtils.getAppUserId(this)));
                break;
            case 2:
                qrcodeText.setText("扫一扫上面的二维码图案，缅信付款码");
                createQRcodeImage("https://a.feigecb.com/?:to".concat(UserInfoUtils.getAppUserId(this)));
                break;
            default:
                qrcodeText.setText("扫一扫上面的二维码图案，加我缅信");
                createQRcodeImage("https://a.feigecb.com/?:add".concat(UserInfoUtils.getUserName(this)));
                break;
        }

    }


    public void createQRcodeImage(String url) {
        im1 = findViewById(R.id.imageView);
        int w = 400;
        int h = 400;
        try {
            //判断URL合法性
            if (url == null || "".equals(url) || url.length() < 1) {
                return;
            }
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //图像数据转换，使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, w, h, hints);
            int[] pixels = new int[w * h];
            //下面这里按照二维码的算法，逐个生成二维码的图片，
            //两个for循环是图片横列扫描的结果
            for (int y = 0; y < h; y++) {
                for (int x = 0; x < w; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * w + x] = 0xff000000;
                    } else {
                        pixels[y * w + x] = 0xffffffff;
                    }
                }
            }
            //生成二维码图片的格式，使用ARGB_8888
            Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
            //显示到我们的ImageView上面
            im1.setImageBitmap(bitmap);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

}
